package com.example.supermarket.controller;

import com.example.supermarket.entity.PageBean;
import com.example.supermarket.entity.ResponseResult;
import com.example.supermarket.entity.dto.AfterSaleDTO;
import com.example.supermarket.entity.dto.AfterSaleTypeDTO;
import com.example.supermarket.entity.vo.AfterSaleVO;
import com.example.supermarket.mapper.AfterSaleMapper;
import com.example.supermarket.service.AfterSaleService;
import com.example.supermarket.util.MyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/afterSale")
public class AfterSaleController {
    @Autowired
    AfterSaleService afterSaleService;
    @Autowired
    AfterSaleMapper afterSaleMapper;
    @Autowired
    MyUtil myUtil;

    @ResponseBody//带条件查询所有信息
    @PostMapping("/get/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<AfterSaleVO>> getAllIndent(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody AfterSaleDTO afterSaleDTO){
        System.out.println(afterSaleDTO);
        return new ResponseResult<PageBean<AfterSaleVO>>(200,"success",
                afterSaleService.getAllAfterSale(pageIndex,pageSize,afterSaleDTO));
    }

    @RequestMapping("/agree/{ddId}/{asStatus}")//审核通过，判断售后类型再写入数据库
    public ResponseResult agree(@PathVariable ("ddId") String ddId, @PathVariable ("asStatus") String asStatus){
        log.info("订单Id={}",ddId);
        String timeNow = myUtil.getTimeNow();
        afterSaleService.agree(ddId, asStatus, timeNow);
        return new ResponseResult(200,"success",null);
    }

    @RequestMapping("/reject/{ddId}/")//审核未通过，更改售后状态(已驳回)
    public ResponseResult changeAsStatusByDdId1(@PathVariable ("ddId") String ddId){
        log.info("订单Id={}",ddId,"已驳回");
        String timeNow = myUtil.getTimeNow();
        afterSaleService.reject(ddId,timeNow);
        return new ResponseResult(200,"success",null);
    }

    @RequestMapping("/dealRequest1/{ddId}/{asStatus}")//退换一体操作进行中
    public ResponseResult dealRequest1(@PathVariable ("ddId") String ddId, @PathVariable ("asStatus") String asStatus){
        log.info("订单Id={}",ddId);
        String timeNow = myUtil.getTimeNow();
        afterSaleService.dealRequest1(ddId,asStatus,timeNow);
        return new ResponseResult(200,"success",null);
    }

    @RequestMapping("/dealRequest2/{ddId}/{asStatus}")//退换一体操作完成后
    public ResponseResult dealRequest2(@PathVariable ("ddId") String ddId, @PathVariable ("asStatus") String asStatus){
        log.info("订单Id={}",ddId);
        String timeNow = myUtil.getTimeNow();
        afterSaleService.dealRequest2(ddId,asStatus,timeNow);
        return new ResponseResult(200,"success",null);
    }

    @RequestMapping("/finAfterSale/{ddId}")//完成售后服务
    public ResponseResult finAfterSale(@PathVariable ("ddId") String ddId){
        log.info("订单Id={}",ddId,"成功退货/换货");
        String timeNow = myUtil.getTimeNow();
        afterSaleService.finAfterSale(ddId,timeNow);
        return new ResponseResult(200,"success",null);
    }

    @RequestMapping("/changeAsType")//添加售后信息
    public ResponseResult<Boolean> changeAfterSaleType(@RequestBody AfterSaleTypeDTO afterSaleTypeDTO) {
        log.info("售后订单={}", afterSaleTypeDTO);
        afterSaleTypeDTO.setAsStatus("待审核");
        //更改售后表
        afterSaleService.changeAfterSaleType(afterSaleTypeDTO);
        //更改订单表的状态
        afterSaleService.changeIndentDdStatus(afterSaleTypeDTO.getDdId());
        return new ResponseResult(200,"success",null);
    }
}
